package app

import (
	"fmt"
	"net/http"
	"os"
	"strings"
)

// 注册静态文件服务
func RegisterStaticFilePath(pattern string, staticpath string) {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		stDir := GetCurDir()
		if strings.HasPrefix(r.URL.Path, staticpath) {
			file := stDir + r.URL.Path
			fmt.Println(r.URL.Path)
			fmt.Println(file)
			f, err := os.Open(file)
			defer f.Close()

			if err != nil && os.IsNotExist(err) {
				fmt.Fprintln(w, "File not exist")
				return
			}
			http.ServeFile(w, r, file)
			return
		} else {
			fmt.Fprintln(w, "Hello world")
		}
	})
}
